import { ElMessage, ElNotification } from 'element-plus'

/**
 * 权限错误处理器
 */
export class PermissionErrorHandler {
  static instance = null
  
  constructor() {
    if (PermissionErrorHandler.instance) {
      return PermissionErrorHandler.instance
    }
    
    this.errorDialogRef = null
    PermissionErrorHandler.instance = this
  }
  
  /**
   * 设置错误弹框引用
   */
  setErrorDialogRef(ref) {
    this.errorDialogRef = ref
  }
  
  /**
   * 处理权限错误
   */
  handlePermissionError(error, options = {}) {
    const {
      showDialog = true,
      showMessage = true,
      showNotification = false,
      autoShowDialog = true
    } = options
    
    // 检查是否有权限详细信息
    const permissionInfo = error.response?.data?.permissionInfo
    const responseData = error.response?.data
    
    if (!permissionInfo) {
      // 没有详细权限信息，使用普通错误处理
      if (showMessage) {
        const message = responseData?.message || responseData?.detail || '权限不足'
        ElMessage.error(message)
      }
      return
    }
    
    // 构造错误信息对象
    const errorInfo = {
      message: responseData.message || '权限不足',
      detail: responseData.detail || '您没有执行此操作的权限',
      permissionInfo: permissionInfo
    }
    
    // 显示通知
    if (showNotification) {
      this.showPermissionNotification(errorInfo)
    }
    
    // 显示消息
    if (showMessage && !showDialog) {
      this.showPermissionMessage(errorInfo)
    }
    
    // 显示弹框
    if (showDialog && autoShowDialog && this.errorDialogRef) {
      this.showPermissionDialog(errorInfo)
    }
    
    return errorInfo
  }
  
  /**
   * 显示权限通知
   */
  showPermissionNotification(errorInfo) {
    const title = '权限不足'
    let message = errorInfo.detail
    
    if (errorInfo.permissionInfo?.permissionDescription) {
      message = `缺少"${errorInfo.permissionInfo.permissionDescription}"权限`
    }
    
    ElNotification({
      title,
      message,
      type: 'error',
      duration: 8000,
      position: 'top-right'
    })
  }
  
  /**
   * 显示权限消息
   */
  showPermissionMessage(errorInfo) {
    let message = errorInfo.detail
    
    if (errorInfo.permissionInfo?.permissionDescription) {
      message = `权限不足：缺少"${errorInfo.permissionInfo.permissionDescription}"权限`
    }
    
    ElMessage({
      message,
      type: 'error',
      duration: 5000,
      showClose: true
    })
  }
  
  /**
   * 显示权限弹框
   */
  showPermissionDialog(errorInfo) {
    if (this.errorDialogRef?.showError) {
      this.errorDialogRef.showError(errorInfo)
    }
  }
  
  /**
   * 检查是否为权限错误
   */
  static isPermissionError(error) {
    return error.response?.status === 403 && error.response?.data?.permissionInfo
  }
  
  /**
   * 获取权限错误信息
   */
  static getPermissionErrorInfo(error) {
    if (!this.isPermissionError(error)) {
      return null
    }
    
    const responseData = error.response.data
    return {
      message: responseData.message || '权限不足',
      detail: responseData.detail || '您没有执行此操作的权限',
      permissionInfo: responseData.permissionInfo
    }
  }
}

// 创建单例实例
export const permissionErrorHandler = new PermissionErrorHandler()

// 默认导出
export default permissionErrorHandler 